package org.itboys.mobile.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.entity.mongo.SundayNewsCategory;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/23
 * 描述：雄安加盟网_资讯
 */
@Service
public class SundayNewsCategoryService extends BaseMongoService<SundayNewsCategory>{
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayNewsCategory> getEntityClass() {
        return SundayNewsCategory.class;
    }

    /**
     * 获取商品类目的类目。树结构类型。
     * @return
     */
    public List<SundayNewsCategory> getCategoryTreeJson(){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("sort","sequence");
        param.put("order","asc");
        List<SundayNewsCategory> all= super.list(param);
        List<SundayNewsCategory> parents = new ArrayList<SundayNewsCategory>();
        List<SundayNewsCategory> children = new ArrayList<SundayNewsCategory>();

        for(SundayNewsCategory c:all){
            if(StringUtils.isNotEmpty(c.getImage())){
                c.setImage(resourceHolder.getStringValue("imgRoot")+c.getImage());
            }else{
                c.setImage(null);
            }

            if(c.getParentId().longValue()==0l){
                parents.add(c);
            }else{
                children.add(c);
            }
        }
        //组装TREE
        for(SundayNewsCategory parent:parents){
            List<SundayNewsCategory> child = new ArrayList<SundayNewsCategory>();
            for(SundayNewsCategory c:children){
                if(LongUtil.isNotZreo(c.getParentId())&&
                        c.getParentId().longValue()==parent.getId()){
                    child.add(c);
                }
            }
            parent.setChildren(child);
        }
        return parents;
    }


    /**
     * 新增或修改分类
     * @param category
     * @param imageFile
     * @throws Exception
     */
    public void save(SundayNewsCategory category, MultipartFile imageFile) throws Exception{
        Map<String, String> map = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){
            category.setImage(map.get(uploadService.UPLOAD_HALF_PATH));
        }
        if(LongUtil.isNotZreo(category.getId())){
            if(LongUtil.isNull(category.getParentId())){
                category.setParentId(0l);
            }
            super.updateExceptEmpty(category.getId(),category);
        }else{
            super.save(category);
        }
    }
    /**
     * 删除商品类目
     * @param categoryId
     */
    public void deleteCategory(Long categoryId){
        super.delete(categoryId);
        logger.info("删除资讯类目","id="+categoryId);
    }

    /**
     * 获取首页需要的资讯分类
     * @return
     */
    public List<SundayNewsCategory> getIndexNewsCategory(){
        Map<String,Object> param=new HashMap<String ,Object>();
        param.put("isDeleted", 0);
        param.put("parentId",0);
        param.put("isIndex",1);
        param.put("order","asc");
        List<SundayNewsCategory> categories=super.list(param);
        return categories;
    }

    /**
     * 获取首页需要的推荐资讯分类
     * @return
     */
    public List<SundayNewsCategory> getIndexRecommendNewsCategory(){
        Map<String,Object> param=new HashMap<String ,Object>();
        param.put("isDeleted", 0);
        param.put("parentId",0);
        param.put("isRecommend",1);
        param.put("order","asc");
        List<SundayNewsCategory> categories=super.list(param);
        return categories;
    }

    /**
     * 设置
     * @param id
     * @param type
     * @param value
     */
    public void set(Long id,Integer type,Integer value){
        SundayNewsCategory category=super.getById(id);
        Map<String,Object> param=new HashMap<String ,Object>();
        //设置或取消首页推荐
        if(type==1){
            if(value==0){
                category.setIsIndex(value);
            }else if(value==1){
                param.put("isDeleted", 0);
                param.put("isIndex",1);
                List<SundayNewsCategory> categories=super.list(param);
                if(ListUtil.isNotNull(categories)&&categories.size()>=4){
                    throw  new ErrorException("首页资讯分类为四个");
                }
                category.setIsIndex(value);
            }

        }
        if(type==2){
            if(value==0){
                category.setIsRecommend(value);
            }else if(value==1){
                param.put("isDeleted", 0);
                param.put("isRecommend",1);
                List<SundayNewsCategory> categories=super.list(param);
                if(ListUtil.isNotNull(categories)&&categories.size()>=3){
                    throw  new ErrorException("推荐资讯分类为三个");
                }
                category.setIsRecommend(value);
            }

        }
        super.updateExceptEmpty(id,category);
    }
}
